<template>
	<view class="order-detail">
		<view class="head">
			<view class="status">
				<view>
					<text v-if="order_detail.status === '1'">等待付款</text>
					<text v-if="order_detail.status === '2'">等待发货</text>
					<text v-if="order_detail.status === '3'">已发货</text>
					<text v-if="order_detail.status === '4'&& afterSale!=4">已收货</text>
					<text v-if="order_detail.status === '5'">交易关闭</text>
					<text v-if="order_detail.status === '6'&& refundStatus==='1' ">等待退款中</text>
					<text v-if="order_detail.status === '6'&& refundStatus==='2' ">退款中</text>
					<text v-if="order_detail.status === '6'&& refundStatus==='3' ">退款成功</text>
					<text v-if="afterSale === '4'&& refundStatus==='1'">退款失败</text>
				</view>
				<view class="tips">
					<text v-if="order_detail.status === '1'">30分钟后自动取消订单</text>
				</view>
			</view>
		</view>
		<block v-for="(item,index) in order_detail_go" :key="index">
			<view class="order">
				<view class="order-item">
					<view class="item-left">
						<view class="img">
							<image :src="item.listImage"></image>
						</view>
					</view>
					<view class="item-right">
						<view class="title">
							<text>{{item.title}}</text>
						</view>
						<view class="price">
							<text>¥</text>
							<text>{{item.unitPrice}}</text>
							<text>×{{item.sum}}</text>
						</view>
					</view>
				</view>
			</view>
		</block>
		<!--订单描述-->
		<view class="order-desc">
			<view class="desc">
				<view class="order-num">
					<text class="title">订单号:</text>
					<text class="content">{{order_detail.orderNumber}}</text>
				</view>
				<view class="order-time">
					<text class="title">下单时间:</text>
					<text class="content">{{order_detail.createTime}}</text>
				</view>
				<view class="line-1"></view>
				<view class="pay-type">
					<text class="title">支付方式:</text>
					<text class="content">在线支付</text>
				</view>
				<view class="pay-time" v-if="order_detail.payTime">
					<text class="title">支付时间:</text>
					<text class="content">{{order_detail.payTime}}</text>
				</view>
			</view>
		</view>
		<!--地址信息-->
		<view class="address-info">
			<view class="address">
				<view class="address-up">
					<text class="title">收货地址:</text>
					<view class="name">
						<text>{{order_detail.receiver}}</text>
						<text>{{order_detail.receiverPhone}}</text>
					</view>
				</view>
				<view class="address-down">
					<view class="address-detail">
						<text>{{order_detail.shippingAddress}}</text>
					</view>
				</view>
			</view>
		</view>
		<!--金额-->
		<view class="price-info">
			<view class="price">
				<view class="title">
					<view class="title-name">
						<text>商品金额</text>
					</view>
					<view class="title-price">
						<text>￥{{shopAmount}}</text>
					</view>
				</view>
				<view class="title">
					<view class="title-name">
						<text>快递运费</text>
					</view>
					<view class="title-price">
						<text>￥{{freight}}</text>
					</view>
				</view>
				<view class="title last-title">
					<view class="title-name">
						<text>优惠券</text>
					</view>
					<view class="title-price">
						<text>-￥{{couponAmoun||0.00}}</text>
					</view>
				</view>
				<view class="real-pay">
					<view>
						<text>实付金额：</text>
						<text>¥{{order_detail.payAmount}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="pay-button">
			<view class="cancel" @click="cancelPay()" v-if="order_detail.status === '1'">
				<text>取消订单</text>
			</view>
			<view class="pay" @click="goPay()" v-if="order_detail.status === '1'">
				<text>付款</text>
			</view>
			<view class="del" @click="delOrder()" v-if="order_detail.status === '5'">
				<text>删除订单</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				order_detail: {},
				order_detail_go: [],
				order_id: '',
				orderNumber:'',
				refundStatus:'',
				//售后状态status
				afterSale:'',
				//优惠金额
				couponAmoun:'',
				//商品金额
				shopAmount:'',
				//运费
				freight:'',
				//实付金额
				payAmount:''
			};
		},
		onLoad: function(option) {
			this.order_id = option.id
			this.orderNumber = option.orderNumber
			//获取订单列表
			this.getDetail(this.order_id)
			//获取售后详情数据
			this.getAftersaleInfo(this.orderNumber)
		},
		methods: {
			//获取售后详情数据
			getAftersaleInfo(order){
				var params = {
					orderNumber:order
				}
				this.$postApi(this.$path.GOODS_AFTERSALE_LIST,params).then(res=>{
					console.log("返回的售后详情-------------->",res)
					if(res.data.success){
						this.refundStatus = res.data.data.list[0].refundStatus
						this.afterSale = res.data.data.list[0].status
					}
				})
			},
			//删除订单
			delOrder: function() {
				let params = {
					id: this.order_id
				}
				uni.showModal({
					title: '删除订单',
					content: '确认删除该订单吗？',
					showCancel: true,
					success: res => {
						if (res.confirm) {
							this.$postApi(this.$path.GOODS_ORDER_DELETE, params).then(res => {
								console.log("删除订单返回结果------------>", res.data)
								if (res.data.success) {
									this.pub.showError('删除成功')
									setTimeout(function() {
										uni.redirectTo({
											url: '/pages/orderlist/orderlist',
											animationType: 'pop-in',
											animationDuration: 200
										});
									}, 2000);
								} else {
									this.pub.showError(res.data.error.errorMessage)
								}
							})
						} else {

						}
					},
				})
			},
			//取消订单
			cancelPay: function() {
				let params = {
					id: this.order_detail.id,
					orderNumber: this.order_detail.orderNumber,
					status: '5'
				}
				uni.showModal({
					title: '取消订单',
					content: '确认取消该订单吗？',
					showCancel: true,
					success: res => {
						this.$postApi(this.$path.GOODS_ORDER_STATUS, params).then(res => {
							console.log("取消订单返回结果------------>", res.data)
							if (res.data.success) {

								this.getDetail(this.order_detail.id)

								this.pub.showError("订单取消成功")
								
							} else {
								this.pub.showError(res.data.error.errorMessage)
							}
						})
					},
				})
			},
			goPay: function() {
				let data = {
					payAmount: this.order_detail.payAmount,
					orderId: this.order_detail.id
				};
				uni.navigateTo({
					url: '/pages/scenicOrder/counter?data=' + JSON.stringify(data) + '&bustype=3',
					animationType: 'pop-in',
					animationDuration: 200
				});
			},
			//获取订单列表
			getDetail: function(order_id) {
				let params = {
					id: order_id
				}
				this.$postApi(this.$path.GOODS_ORDER_DETAIL, params).then(res => {
					console.log("订单详情返回------------->", res.data)
					if (res.data.success) {
						this.order_detail = res.data.data
						this.payAmount = this.order_detail.payAmount
						this.freight = this.order_detail.freight
						this.couponAmoun = this.order_detail.couponAmoun
						this.order_detail_go = res.data.data.goodsOrderDetailList
						if(!this.couponAmoun){
							this.shopAmount = parseFloat(this.payAmount)-parseFloat(this.freight)
						}else{
							this.shopAmount = parseFloat(this.payAmount)-parseFloat(this.freight)+parseFloat(this.couponAmoun)
						}
					} else {
						this.pub.showError(res.data.error.errorMessage)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.order-detail {
		padding: 0;
		margin: 0;
		min-height: 100%;
		background: #f2f2f2;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.head {
		height: 200upx;
		background: linear-gradient(87deg, rgba(249, 140, 45, 1) 0%, rgba(250, 108, 46, 1) 100%);
		padding: 40upx;
		display: flex;
		align-items: center;

		.status {
			view:first-child {
				font-size: 36upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: rgba(255, 255, 255, 1);
			}

		}

		.status .tips {
			font-size: 24upx;
			font-family: PingFang SC;
			font-weight: 300;
			color: rgba(255, 255, 255, 1);

		}
	}

	.order {
		padding: 30upx;
		background: #fff;
	}

	.order-item {
		display: flex;

		.item-left {
			width: 20%;
		}

		.img {
			width: 112upx;
			height: 112upx;
			border-radius: 7upx;

			image {
				border-radius: 7upx;
			}
		}

		.item-right {
			width: 80%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
		}

		.title {
			font-size: 34upx;
			font-family: PingFang SC;
			font-weight: bold;
			color: rgba(51, 51, 51, 1);
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		.price {
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: bold;
			color: rgba(255, 94, 4, 1);
			line-height: 42upx;
		}

		.price text:last-child {
			font-size: 28upx;
			font-family: PingFang SC;
			font-weight: 500;
			color: rgba(102, 102, 102, 1);
			line-height: 42upx;
			margin-left: 5upx;
		}

	}

	.order-desc {
		padding: 30upx;
		background: #fff;
		margin-top: 20upx;

		.desc .title {
			font-size: 26upx;
			font-family: PingFang SC;
			font-weight: bold;
			color: rgba(77, 77, 77, 1);
			line-height: 34px;
			letter-spacing: 4upx;
		}

		.line-1 {
			height: 1upx;
			width: 100%;
			border: 1upx solid #F4F4F4;
		}

		.desc .content {
			font-size: 26upx;
			margin-left: 14upx;
			font-family: PingFang SC;
			color: rgba(77, 77, 77, 1);
			line-height: 34px;
		}
	}

	.address-info {
		padding: 30upx;
		background: #fff;
		margin-top: 20upx;

		.address {
			display: flex;
			flex-direction: column;

		}

		.address-up {
			display: flex;
			align-items: center;
		}

		.address-up .title {
			font-size: 28upx;
			font-family: PingFang SC;
			font-weight: bold;
			color: rgba(77, 77, 77, 1);
			line-height: 34upx;
			letter-spacing: 4upx;
		}

		.name {
			font-size: 28upx;
			margin-left: 10upx;
			font-family: PingFang SC;
			font-weight: bold;
			color: rgba(26, 26, 26, 1);
			line-height: 37upx;
		}

		.name text:last-child {
			margin-left: 10upx;
		}

		.address-down {
			margin-top: 20upx;
			display: flex;
			justify-content: flex-end;

			.address-detail {
				width: 79%;
				font-size: 24upx;
				font-family: PingFang SC;
				// font-weight:300;
				color: rgba(26, 26, 26, 1);
				line-height: 33upx;
			}
		}
	}

	.price-info {
		padding: 30upx;
		background: #fff;
		margin-top: 20upx;
		.last-title{
			border-bottom: 1upx solid #f5f5f5;
		}
		.title {
			display: flex;
			justify-content: space-between;
			
			padding-bottom: 20upx;
			
			.title-name {
				font-size: 29upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: rgba(77, 77, 77, 1);
			}

			.title-price {
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color:rgba(51,51,51,1);
			}
		}

		.real-pay {
			margin-top: 30upx;
			display: flex;
			justify-content: flex-end;
			width: 100%;
		}

		.real-pay view {
			// width: 30%;

			text:first-child {
				font-size: 29upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: rgba(77, 77, 77, 1);
			}

			text:last-child {
				font-size: 34upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FF5E04;
			}
		}
	}

	.pay-button {
		padding: 30upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;

		view {
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 26upx;
			width: 159upx;
			height: 55upx;
			border: 1px solid rgba(204, 204, 204, 1);
			border-radius: 28upx;
			margin-left: 16upx;
			letter-spacing: 3upx;
		}

		.cancel,
		.del {
			color: rgba(128, 128, 128, 1);
		}

		.pay {
			color: rgba(255, 94, 4, 1);
			border: 1px solid rgba(255, 94, 4, 1);
		}
	}
</style>
